$(function () { 
	$.fn.extend({
		loadCombox:function() {
			this.find("input[type=comboxSelect]").each(function(index,_this){
				var select_id = $(_this).attr("id");
				var select_value = $(_this).attr("value");
				var select_loadUrl = $(_this).attr("loadUrl");
				$(_this).hide();
				var selectStr ='<select  id="'+select_id+'_selects" class="col-xs-10 col-sm-5"></select>';
				$(_this).after(selectStr);
				$.post(select_loadUrl,{},function(data){
					var json = $.parseJSON(data);
					console.log(json.obj);
					console.log(json.resMsg);
					var comboxJson = json.obj;
					var optionStr='<option value="-1">请选择</option>';
					for(var i=0;i<comboxJson.length;i++){
						var selected="";
						if(select_value == '' || select_value == null){
							selected+='selected="selected"';
						}
							optionStr += '<option value="'+comboxJson[i].hymc+'" '+selected+'>'+comboxJson[i].hymc+'</option>';
					}
					$("#"+select_id+"_selects").append(optionStr);
				});
				$("#"+select_id+"_selects").change(function(){
					var onVal = $(this).val();
					$(_this).attr("value",onVal);
				});
				
			});
		},	loadCombox1:function() {
			this.find("input[type=comboxSelect]").each(function(index,_this){
				var select_id = $(_this).attr("id");
				var select_value = $(_this).attr("value");
				var select_loadUrl = $(_this).attr("loadUrl");
				$(_this).hide();
				var selectStr ='<select  id="'+select_id+'_selects" class="col-xs-10 col-sm-5"></select>';
				$(_this).after(selectStr);
				$.post(select_loadUrl,{},function(data){
					var json = $.parseJSON(data);
					console.log(json.obj);
					console.log(json.resMsg);
					var comboxJson = json.obj;
					var optionStr='<option value="-1">请选择</option>';
					for(var i=0;i<comboxJson.length;i++){
						var selected="";
						if(select_value == '' || select_value == null){
							selected+='selected="selected"';
						}
							optionStr += '<option value="'+comboxJson[i].lmmc+'" '+selected+'>'+comboxJson[i].lmmc+'</option>';
					}
					$("#"+select_id+"_selects").append(optionStr);
				});
				$("#"+select_id+"_selects").change(function(){
					var onVal = $(this).val();
					$(_this).attr("value",onVal);
				});
				
			});
		},	
		loadFY:function(json,cl,params,url){
			$("#fy").remove();
			var _tabl_this=this;
			_tabl_this.after('<div class="modal-footer no-margin-top" style="display:none" id="fy"></div>');
			$("#fy").append('<ul class="pagination pull-right no-margin"></ul>');
			var nowpageNum = json.obj.pageNum;
			//var nowpageSize = json.obj.pageSize;
			var tR = json.obj.totalRecord;
			if(tR > 0){
				$("#fy").show();
				var prevDisabled = "";
				if(nowpageNum==1){
					prevDisabled = "disabled";
				}
				var fyLi='<li class="prev '+prevDisabled+'" id="diyiye"><a href="javascript:void(0);" onclick="">';
				fyLi += '第一页</a></li>';
				fyLi +='<li class="prev '+prevDisabled+'" id="shangyiye"><a href="javascript:void(0);" onclick="">';
				fyLi += '<i class="icon-double-angle-left"></i></a></li>';
				for(var i=1;i<=json.obj.totalPage;i++){
					
					var acStr='';
					if(json.obj.pageNum==i){
						acStr='active';
					}
						fyLi+='<li class="'+acStr+' dangqian" name="'+i+'"><a href="javascript:void(0);" onclick="">'+i+'</li>';
					
				}
				var nextDisabled = "";
				if(nowpageNum==json.obj.totalPage){
					nextDisabled = "disabled";
				}
				fyLi += '<li class="next '+nextDisabled+'" id="xiayiye"><a href="javascript:void(0);" onclick="">';
				fyLi += '<i class="icon-double-angle-right"></i></a></li>';
				fyLi += '<li class="next '+nextDisabled+'" id="zuihouye"><a href="javascript:void(0);" onclick="">';
				fyLi += '最后页</a></li>';
				$("#fy>ul").find("li").remove();
				$("#fy>ul").append(fyLi);
				
				$("#diyiye").click(function(){
					params.pageNum = 1;
					_tabl_this.loadTbl(cl,params,url,true);
				});
				$("#shangyiye").click(function(){
					if(params.pageNum>1){
						params.pageNum = params.pageNum-1;
						_tabl_this.loadTbl(cl,params,url,true);
					}
				});
				$(".dangqian").click(function(){
					var i = $(this).attr("name");
					params.pageNum = i;
					_tabl_this.loadTbl(cl,params,url,true);
				});
				$("#xiayiye").click(function(){
					if(json.obj.totalRecord>params.pageNum){
						params.pageNum = params.pageNum+1;
						_tabl_this.loadTbl(cl,params,url,true);
					}
				});
				$("#zuihouye").click(function(){
					params.pageNum = json.obj.totalRecord;
					_tabl_this.loadTbl(cl,params,url,true);
				});
				
			}
		},
		loadTbl:function(cl,params,url,isFY){
			var _tabl_this=this;
			$.post(url,params,function(data){
				var json = $.parseJSON(data);
				if(json.res=="1"){
					var result = json.obj.results;
					if(result==null){
						result=json.obj;
					}
					var tdStr = '<tr>';
					for(var i=0;i<result.length;i++){
						for(var j=0;j<cl.length;j++){
							var clOne = cl[j].name;
							var clTwo = cl[j].cl;
							var tbl_html = '';
							if(typeof clTwo == 'function' && clTwo != null){
								tbl_html = clTwo(j,result[i]);
							}
							if(tbl_html != ""){
								tdStr += '<td>'+tbl_html+'</td>';
							}else{
								tdStr += '<td>'+result[i][""+clOne+""]+'</td>';
							}
						}
						tdStr += '</tr>';
					}
					_tabl_this.children("tbody").eq(0).children().remove();
					_tabl_this.children("tbody").eq(0).append(tdStr);
					
					if(isFY){
						_tabl_this.loadFY(json,cl,params,url);
					}
				}
			});
		}
	});
	$.fn.extend({
		loadComboxDOM:function(formId) {
			$("#formId").find("input[on_type=comboxSelect]").each(function(index,_this){
				var select_id = $(_this).attr("id");
				var select_value = $(_this).attr("value");
				var select_loadUrl = $(_this).attr("loadUrl");
				//$(_this).hide();
				var selectStr ='<select  id="'+select_id+'_selects" class="col-xs-10 col-sm-5"></select>';
				$(_this).after(selectStr);
				$.post(select_loadUrl,{},function(data){
					var json = $.parseJSON(data);
					console.log(json.obj);
					console.log(json.resMsg);
					var comboxJson = json.obj;
					var optionStr='<option value="-1">请选择</option>';
					for(var i=0;i<comboxJson.length;i++){
						var selected="";
						if(select_value == '' || select_value == null){
							selected+='selected="selected"';
						}
							optionStr += '<option value="'+comboxJson[i].id+'" '+selected+'>'+comboxJson[i].hymc+'</option>';
					}
					$("#"+select_id+"_selects").append(optionStr);
				});
				$("#"+select_id+"_selects").change(function(){
					var onVal = $(this).val();
					$(_this).attr("value",onVal);
				});
				
			});
		}	
	});
	
});